﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using shenhao.web.Models;
using shenhao.web.Repository;

namespace shenhao.web.Controllers
{
    public class TemperatureController : Controller
    {

        private readonly TemperatureRepository repository;

        public TemperatureController(TemperatureRepository repository)
        {
            this.repository = repository;
        }

        #region 查询

        /// <summary>
        /// 获取所有或则获取当天所有。
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public JsonResult GetAll(string date = "")
        {
            try
            {
                List<Tempertaure> tempertaures = new List<Tempertaure>();
                if (string.IsNullOrEmpty(date))
                {
                    tempertaures = repository.Load(t => true).ToList();
                }
                else
                {
                    tempertaures = repository.Load(t => t.DateTime.Date == Convert.ToDateTime(date).Date).ToList();
                }

                return AjaxResult.Success(data: tempertaures);
            }
            catch (Exception ex)
            {
                return AjaxResult.Error(ex.Message);
            }
        }

        [HttpGet]
        public JsonResult Get(DateTime dateTime)
        {
            try
            {
                Tempertaure tempertaure = repository.Load(t => t.DateTime == dateTime).FirstOrDefault();
                return AjaxResult.Success(data: tempertaure);
            }
            catch (Exception ex)
            {
                return AjaxResult.Error(ex.Message);
            }
        }

        [HttpGet]
        public JsonResult GetById(Guid guid)
        {
            try
            {
                Tempertaure tempertaure = repository.Load(t => t.Guid == guid).FirstOrDefault();
                return AjaxResult.Success(data: tempertaure);
            }
            catch (Exception ex)
            {
                return AjaxResult.Error(ex.Message);
            }
        }


        #endregion



        [HttpPost]
        public JsonResult Insert(DateTime dateTime, float value)
        {
            try
            {
                Tempertaure tempertaure = new Tempertaure
                {
                    Guid = Guid.NewGuid(),
                    DateTime = dateTime,
                    Value = value
                };
                tempertaure = repository.AddEntity(tempertaure, true);
                return AjaxResult.Success(data: tempertaure);
            }
            catch (Exception ex)
            {
                return AjaxResult.Error(ex.Message);
            }
        }

        [HttpPost]
        public JsonResult Update(Tempertaure tempertaure)
        {
            try
            {
                if (tempertaure.Guid == null || tempertaure.Guid == Guid.Empty || tempertaure.DateTime == DateTime.MinValue)
                {
                    return AjaxResult.Error("数据有误！");
                }
                tempertaure = repository.Update(tempertaure);
                return AjaxResult.Success(data: tempertaure);
            }
            catch (Exception ex)
            {
                return AjaxResult.Error(ex.Message);
            }
        }

        #region 删除


        public JsonResult DeleteById(Guid guid)
        {
            try
            {
                if (guid == null || guid == Guid.Empty)
                {
                    return AjaxResult.Error("数据有误！");
                }
                Tempertaure tempertaure = repository.Load(t => t.Guid == guid).FirstOrDefault();
                if (tempertaure != null)
                {
                    bool success = repository.Remove(tempertaure);
                    return AjaxResult.Success(success ? "删除成功！" : "删除失败！");
                }
                return AjaxResult.Error("没有该数据！");
            }
            catch (Exception ex)
            {
                return AjaxResult.Error(ex.Message);
            }
        }

        public JsonResult Delete(DateTime dateTime)
        {
            try
            {
                if (dateTime == DateTime.MinValue)
                {
                    return AjaxResult.Error("数据有误！");
                }
                Tempertaure tempertaure = repository.Load(t => t.DateTime == dateTime).FirstOrDefault();
                if (tempertaure != null)
                {
                    bool success = repository.Remove(tempertaure);
                    return AjaxResult.Success(success ? "删除成功！" : "删除失败！");
                }
                return AjaxResult.Error("没有该数据！");
            }
            catch (Exception ex)
            {
                return AjaxResult.Error(ex.Message);
            }
        }


        #endregion



    }
}